<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="序号" prop="sequenceNumber">
        <el-input v-model="formData.sequenceNumber" placeholder="请输入序号" />
      </el-form-item>
       <el-form-item label="退货单号码" prop="returnNo">
        <el-input v-model="formData.returnNo" placeholder="请输入退货单号码" />
      </el-form-item>
      <el-form-item label="仓库id" prop="warehouseId">
        <el-input v-model="formData.warehouseId" placeholder="请输入仓库id" />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input v-model="formData.warehouseName" placeholder="请输入仓库名称" />
      </el-form-item>
      <el-form-item label="收货单id" prop="receivingId">
        <el-input v-model="formData.receivingId" placeholder="请输入收货单id" />
      </el-form-item>
      <el-form-item label="收货单号" prop="receivingNo">
        <el-input v-model="formData.receivingNo" placeholder="请输入收货单号" />
      </el-form-item>
      <el-form-item label="物料id" prop="materialId">
        <el-input v-model="formData.materialId" placeholder="请输入物料id" />
      </el-form-item>
      <el-form-item label="物料编号" prop="materialCode">
        <el-input v-model="formData.materialCode" placeholder="请输入物料编号" />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="formData.materialName" placeholder="请输入物料名称" />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="formData.unit" placeholder="请输入单位" />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input v-model="formData.specification" placeholder="请输入规格" />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input v-model="formData.model" placeholder="请输入型号" />
      </el-form-item>
      <el-form-item label="退货数" prop="quantityReturned">
        <el-input v-model="formData.quantityReturned" placeholder="请输入退货数" />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input v-model="formData.unitPrice" placeholder="请输入单价" />
      </el-form-item>
      <el-form-item label="退货金额" prop="returnAmount">
        <el-input v-model="formData.returnAmount" placeholder="请输入退货金额" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="月结" prop="monthlySettlement">
        <el-radio-group v-model="formData.monthlySettlement">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="科目id" prop="subjectId">
        <el-input v-model="formData.subjectId" placeholder="请输入科目id" />
      </el-form-item>
      <el-form-item label="科目编号" prop="subjectCode">
        <el-input v-model="formData.subjectCode" placeholder="请输入科目编号" />
      </el-form-item>
      <el-form-item label="科目名称" prop="subjectName">
        <el-input v-model="formData.subjectName" placeholder="请输入科目名称" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { StockMaterialReturnApi } from '@/api/erp/stockmaterialreturn'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  sequenceNumber: undefined,
  returnId: undefined,
  returnNo: undefined,
  warehouseId: undefined,
  warehouseName: undefined,
  receivingId: undefined,
  receivingNo: undefined,
  materialId: undefined,
  materialCode: undefined,
  materialName: undefined,
  unit: undefined,
  specification: undefined,
  model: undefined,
  quantityReturned: undefined,
  unitPrice: undefined,
  returnAmount: undefined,
  remark: undefined,
  monthlySettlement: undefined,
  subjectId: undefined,
  subjectCode: undefined,
  subjectName: undefined
})
const formRules = reactive({
  returnId: [{ required: true, message: '退货单id不能为空', trigger: 'blur' }],
  monthlySettlement: [{ required: true, message: '月结不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number, returnId: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.returnId = returnId
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await StockMaterialReturnApi.getStockMaterialReturnDetail(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await StockMaterialReturnApi.createStockMaterialReturnDetail(data)
      message.success(t('common.createSuccess'))
    } else {
      await StockMaterialReturnApi.updateStockMaterialReturnDetail(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    sequenceNumber: undefined,
    returnId: undefined,
    returnNo: undefined,
    warehouseId: undefined,
    warehouseName: undefined,
    receivingId: undefined,
    receivingNo: undefined,
    materialId: undefined,
    materialCode: undefined,
    materialName: undefined,
    unit: undefined,
    specification: undefined,
    model: undefined,
    quantityReturned: undefined,
    unitPrice: undefined,
    returnAmount: undefined,
    remark: undefined,
    monthlySettlement: undefined,
    subjectId: undefined,
    subjectCode: undefined,
    subjectName: undefined
  }
  formRef.value?.resetFields()
}
</script>